package com.cloudfun.campusshare.entity;

import cn.hutool.core.bean.BeanUtil;
import com.cloudfun.campusshare.common.constant.AnnouncementType;
import com.cloudfun.campusshare.service.UserRelService;
import com.cloudfun.campusshare.util.SpringBeanUtil;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * @author : zhangyongjie
 * @version : 1.0
 * @createTime : 2020/6/5 9:48
 * @description :
 */
@Data
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Table(name = "announcement")
public class AnnouncementEntity extends CreatorPersistenceEntity{
    @Id
    @GenericGenerator(name = "id_uuid", strategy = "uuid")
    @GeneratedValue(generator = "id_uuid")
    @Column(name = "id", unique = true, columnDefinition = ("varchar(40) not null comment '主键uuid'"))
    private String id;


    @Column(name = "type", columnDefinition = ("varchar(32) not null comment '公告类型'"))
    @Enumerated(EnumType.STRING)
    private AnnouncementType type;

    @Column(name = "wash_point_id", columnDefinition = ("varchar(40) default null comment '洗衣点Id'"))
    private String washPointId;

    @Column(name = "wash_point_name", columnDefinition = ("varchar(64) default null comment '洗衣点名称'"))
    private String washPointName;

    @Column(name = "title", columnDefinition = ("varchar(64) default null comment '标题'"))
    private String title;

    @Column(name = "begin_time", columnDefinition = ("datetime default null comment '公告开始时间'"))
    private LocalDateTime beginTime;

    @Column(name = "end_time", columnDefinition = ("datetime default null comment '公告结束时间'"))
    private LocalDateTime endTime;

    @Column(name = "public_time", columnDefinition = ("datetime default null comment '公告发布时间'"))
    private LocalDateTime publicTime;

    @Column(name = "content", columnDefinition = ("longtext default null comment '公告富文本内容'"))
    private String content;

    @Column(name = "is_using", columnDefinition = ("varchar(40) default 1 comment '是否上线'"))
    private Boolean isUsing;

    @Column(name = "merchant_id", columnDefinition = ("varchar(40) not null comment '商家id'"))
    private String merchantId;

    @Column(name = "belong_merchant_name", columnDefinition = ("varchar(64) not null comment '所属商家名称'"))
    private String belongMerchantName;

    @Override
    protected void extraOperate() {
        UserRelService userRelService = SpringBeanUtil.getBean(UserRelService.class);
        UserEntity rootUser = userRelService.getUserMerchant(getCreatorId());
        this.merchantId = rootUser.getId();
        this.belongMerchantName = rootUser.getEnterpriseName();
    }
}
